/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sysype;

/**
 *
 * @author 075071mi
 */
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;




/**
 * @author   Administrador
 */
public class ConexaoMySQL {
	//Icones Icon = new Icones();
	public java.sql.Connection con;
	ResultSet rs = null ;
	//LeAquivoConexao L = new LeAquivoConexao();

	public void Conexao() {
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/bd_ipe", "root","");
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
    }

	// Mtodo para retornar a conexo ------------------------------------------
	public java.sql.Connection getConexao(){
		return con;
	}

	// Mtodo para fechar conexo: parmetro ResultSet -------------------------
	public void fechar(ResultSet rs){
		if(rs != null){
			try{
				rs.close();
			}
			catch(Exception e){}
		}
	}

	// Mtodo para fechar conexo: parmetro java.sql.Statement ----------------
	public void fechar(java.sql.Statement stmt){
		if(stmt != null){
			try{
				stmt.close();
			}
			catch(Exception e){}
		}
	}

	// Mtodo para fechar conexo: sem parmetro -------------------------------
	public void fecharConexao(){
		if(con != null){
			try{
				con.close();
			}
			catch(Exception e){}
		}
	}

	public ResultSet preencheDataSet(String sentenca, int opcao){
		try{
			getConexao();
			Statement stmt = con.createStatement();
			if (opcao == 1)
				rs = (stmt.executeQuery(sentenca));//s select
			if (opcao == 2){
				stmt.executeUpdate(sentenca);
				rs = (stmt.getResultSet());
			}
			return rs;

		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null,e);
			return (null);
		}
	}
}

